package server;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;

public class Main {
	
	/**
	 * Sets the security policy
	 * Starts the registry
	 * implements the object to be exported
	 * Rebinds the specified name to a new remote object 
	 * @param args
	 */
	public static void main(String[] args) {

		String portNum, registryURL, hostname = "tcm-lptp";
												//"192.168.1.66";
		try {
			System.setProperty("java.security.policy", "java.policy");
			System.setProperty("java.rmi.server.hostname", hostname);
			portNum = "1099";
			int RMIportnum = Integer.parseInt(portNum);
			startRegistry(RMIportnum);
			CallbackHandler exportedObject = new CallbackHandler();
			registryURL = String.format("rmi://%s:%s/eexcel", hostname, portNum);
			Naming.rebind(registryURL, exportedObject);
			System.out.println("EExcel server ready");
		} catch (Exception e) {
			//System.out.println("Exception in Main.main: " + e);
			e.printStackTrace();
		}
	}

	/**
	 * Obtains the reference of a remote object on a particular host or create a
	 * remote object that accepts calls on a specific port
	 * 
	 * @param rMIportnum
	 *            Port number
	 * @throws RemoteException
	 *             A RemoteException instance
	 */
	private static void startRegistry(int rMIportnum) throws RemoteException {
		try {
			Registry reg = LocateRegistry.getRegistry(rMIportnum);
			reg.list();
			// This call will throw an exception
			// if the registry does not already exist
		} catch (RemoteException e) {
			Registry reg = LocateRegistry.createRegistry(rMIportnum);
		}
	}
}
